//
//  YSPCollectionViewCell.swift
//  EmotionIcon
//
//  Created by 朴裕识 on 15/12/29.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPCollectionViewCell: UICollectionViewCell {
    
    var emoticonModel: YSPEmoticonModel? {
        didSet {
            if let pngPath = emoticonModel?.pngPath {
                emoticonButton.setImage(UIImage(named: pngPath), forState: UIControlState.Normal)
            } else {
                //防止cell复用
                emoticonButton.setImage(nil, forState: UIControlState.Normal)
            }
            emoticonButton.setTitle(emoticonModel?.emoji, forState: UIControlState.Normal)
            if emoticonModel!.removeEmoticon {
                emoticonButton.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Normal)
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
        // 添加子控件
        contentView.addSubview(emoticonButton)
        
        // 设置尺寸
        // rect: 原始的frame
        // dx: x方向偏移
        // dy: y方向偏移
        // 千万不要使用frame去偏移
        emoticonButton.frame = CGRectInset(bounds, 3, 3)
        emoticonButton.backgroundColor = UIColor.whiteColor()
    }
    
    private lazy var emoticonButton: UIButton = {
        let button = UIButton()
        button.titleLabel?.font = UIFont.systemFontOfSize(32)
        button.userInteractionEnabled = false
        return button
    }()
}
